package cn.tedu._07springaop.egaop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class CartAspect {
    @Before(value ="execution(public boolean cn.tedu._07springaop.egaop.CartServiceImpl.*(..))")
    public void beforeMethod(){
        System.out.println("开始添加购物车");
    }
    @AfterReturning(value = "execution(public boolean cn.tedu._07springaop.egaop.CartServiceImpl.*(..))",returning = "rs")
    public void afterReturningMethod(JoinPoint joinPoint, boolean rs){
        if (rs) {
            System.out.println("添加购物车成功");
        }
        System.out.println("添加购物车失败");
    }
    @After(value = "execution(public boolean cn.tedu._07springaop.egaop.CartServiceImpl.*(..))")
    public void afterMethod(){
        System.out.println("操作完成");
    }

    @Around(value = "execution(public boolean cn.tedu._07springaop.egaop.CartServiceImpl.*(..))")
    public void AroundMethod(ProceedingJoinPoint joinPoint){
        boolean rs = false;
        try {
            System.out.println("开始添加购物车");
            rs = (boolean) joinPoint.proceed();
            if (rs) {
                System.out.println("添加购物车成功");
            }
            System.out.println("添加购物车失败");
        }catch (Throwable e){
            System.out.println("异常通知");
        }finally {
            System.out.println("操作完成");
        }
    }
}
